package com.lakala.bank
{

	import com.as3xls.biff.Record;
	import com.as3xls.xls.Cell;
	import com.as3xls.xls.ExcelFile;
	import com.as3xls.xls.Sheet;
	import org.aswing.plaf.*;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.utils.ByteArray;
	
	import mx.utils.Base64Decoder;
	
	import org.aswing.*;
	import org.aswing.air.JNativeFrame;
	import org.aswing.border.*;
	import org.aswing.colorchooser.*;
	import org.aswing.event.AWEvent;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.table.DefaultTableModel;
	import org.aswing.util.StringUtils;

	/**
	 * MainPanel
	 */
	public class MainPanel extends JPanel
	{
		private var header:Array=["客户名称", "客户编号", "性别", "年龄", "职位", "公司名称", "联系电话", "公司电话", "电子邮件", "公司地址", "家庭地址", "QQ/MSN"];
		private var tableModel:DefaultTableModel;

		private var filter:FileFilter=new FileFilter("文本文件(制表符分隔)", "*.txt");
		private var fileList:FileReferenceList=new FileReferenceList();
		private var recordArr:Array;

//		private var xls:ExcelFile;

		//members define
		private var btnImport:JButton;
		private var btnDelete:JButton;
		private var lbMerAmtTotal:JLabel;
		private var btnExport:JButton;
		private var btnAbout:JButton;
		private var scrollpane3:JScrollPane;
		private var table:JTable;
		
		

		/**
		 * MainPanel Constructor
		 */
		public function MainPanel()
		{
			setSize(new IntDimension(800, 600));
			var layout0:EmptyLayout=new EmptyLayout();
			setLayout(layout0);

			btnImport=new JButton();
			btnImport.setLocation(new IntPoint(5, 5));
			btnImport.setSize(new IntDimension(100, 22));
			btnImport.setText("录入客户资料");
//			btnImport.addActionListener(onImport);
			btnImport.addActionListener(onImportCSV);
			
			btnDelete=new JButton();
			btnDelete.setLocation(new IntPoint(115, 5));
			btnDelete.setSize(new IntDimension(100, 22));
			btnDelete.setText("录入谈话内容");
			btnDelete.addActionListener(onImportExe);

			lbMerAmtTotal=new JLabel();
			lbMerAmtTotal.setFont(new ASFont("Tahoma", 11, true, false, false, false));
			lbMerAmtTotal.setLocation(new IntPoint(140, 5));
			lbMerAmtTotal.setSize(new IntDimension(160, 20));
			lbMerAmtTotal.setHorizontalAlignment(AsWingConstants.RIGHT);

			btnExport=new JButton();
			btnExport.setLocation(new IntPoint(695, 5));
			btnExport.setSize(new IntDimension(100, 22));
			btnExport.setText("其他");
			btnExport.addActionListener(onExport);
			btnAbout=new JButton();
			btnAbout.setLocation(new IntPoint(395, 5));
			btnAbout.setSize(new IntDimension(100, 22));
			btnAbout.setText("关于");
			btnAbout.addActionListener(onAbout);

			scrollpane3=new JScrollPane();
			scrollpane3.setLocation(new IntPoint(0, 40));
			scrollpane3.setSize(new IntDimension(800, 550));

			table=new JTable();
			table.setBackground(new ASColorUIResource(0xB0C4DE, 0.3))
			table.setSize(new IntDimension(800, 550));
			table.setSelectionMode(JTable.SINGLE_SELECTION);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			table.setRowHeight(21);
			tableModel=new DefaultTableModel();
			tableModel.initWithDataNames(new Array(), header);
			table.setModel(tableModel);

			//component layoution
			append(btnImport);
			append(lbMerAmtTotal);
			append(btnDelete);
			append(btnExport);
			append(btnAbout);
			append(scrollpane3);

			scrollpane3.append(table);

		}

		private function onImportCSV(e:AWEvent):void
		{
			var panel:AddCustemerInfo=new AddCustemerInfo();
			var frame:JFrame=new JFrame(this,"录入客户资料",true);
			frame.setResizable(false);
			frame.setIcon(new LoadIcon("asset/search16.png"));
			frame.setSizeWH(710, 410);
			frame.setLocationXY(15, 30);
			frame.getContentPane().append(panel);
			frame.show();
		}
		private function onAbout(e:AWEvent):void
		{
			var panel:AboutInfo=new AboutInfo();
			var frame:JFrame=new JFrame(this,"版权信息",true);
			frame.setResizable(false);
			frame.setIcon(new LoadIcon("asset/lkl16.png"));
			frame.setSizeWH(210, 160);
			frame.setLocationXY(310, 110);
			frame.getContentPane().append(panel);
			frame.show();
			this.tableModel.addRow(new Array("123","123"));
		}
		private function onImportExe(e:AWEvent):void
		{
			var file:File=new File();
			file.addEventListener(Event.SELECT, importCSV);
			file.browseForOpen("选择（制表符分隔）TXT格式的清算文件", [filter]);
		}

		private function importCSV(e:Event):void
		{
			var file:File=new File((e.target as File).nativePath);
			getFileContent(file);
		}

		private function getFileContent(file:File):void
		{
			var fs:FileStream=new FileStream();
			fs.open(file, FileMode.READ);
			var b1:int=fs.readByte();
			var b2:int=fs.readByte();
			fs.position=0;
			var code:String;
			switch (true)
			{
				case (b1 == -1 && b2 == -2):
					code="unicode";
					break;
				case (b1 == -2 && b2 == -1):
					code="unicodeFFFE";
					break;
				case (b1 == -17 && b2 == -69):
					code="utf-8";
					break;
				default:
					code="ansi";
					break;
			}
			var fileContent:String=fs.readMultiByte(fs.bytesAvailable, code);
			var arr:Array=fileContent.split("\n");
			recordArr=new Array();
			for each (var str:String in arr)
			{
				str=StringUtils.replace(str, ",", "");
				var p:Array=str.split("\t");
				if(p.length==14&&StringUtils.startsWith(String(p[0]),"201")){
					this.tableModel.addRow(new Array(p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10],p[11],p[12],p[13]));
					var r:com.lakala.bank.Record=new com.lakala.bank.Record();
					r.start=p[0];
					r.end=p[1];
					r.nick=p[2];
					r.prj=p[3];
					r.num=p[4];
					r.amt=p[5];
					r.amt1=p[6];
					r.unFee=p[7];
					r.unAmt=p[8];
					r.merFee=p[9];
					r.merAmt=p[10];
					r.bank=p[11];
					r.account=p[12];
					r.accountNo=p[13];
					recordArr.push(r);
				}
			}
		}

		private function onImport(e:AWEvent):void
		{
			fileList.browse([filter]);
			fileList.addEventListener(Event.SELECT, fileSelected);
		}

		private function fileSelected(e:Event):void
		{
			var f:FileReference=FileReference(fileList.fileList[0]);
			f.load();
			f.addEventListener(ProgressEvent.PROGRESS, onProgress);
//			f.addEventListener(Event.COMPLETE, onComplete);
		}

		private function onProgress(e:ProgressEvent):void
		{
		}

		/*
				private function onComplete(e:Event):void
				{
					var dec:Base64Decoder=new Base64Decoder();
					var f:FileReference=e.target as FileReference;
					xls=new ExcelFile();
					xls.loadFromByteArray(f.data);
					var sheet:Sheet=(xls.sheets[0] as Sheet);
					recordArr=new Array();
					tableModel.setRowCount(0);

					var total:Number=0;
					for (var i:int=0; i < sheet.rows; i++)
					{
						var rd:com.lakala.bank.Record=new com.lakala.bank.Record();
						for (var j:int=0; j < sheet.cols; j++)
						{
							var cell:com.as3xls.xls.Cell=sheet.getCell(i, j);
							switch (j)
							{
								case 0:
									rd.start=cell.toString();
									break;
								case 1:
									rd.end=cell.value;
									break;
								case 2:
									rd.nick=cell.value;
									break;
								case 3:
									rd.prj=cell.value;
									break;
								case 4:
									rd.num=cell.value;
									break;
								case 5:
									rd.amt=cell.value;
									break;
								case 6:
									rd.amt1=cell.value;
									break;
								case 7:
									rd.unFee=cell.value;
									break;
								case 8:
									rd.unAmt=cell.value;
									break;
								case 9:
									rd.merFee=cell.value;
									break;
								case 10:
									rd.merAmt=cell.value;
									break;
								case 11:
									rd.bank=cell.value;
									break;
								case 12:
									rd.account=cell.value;
									break;
								case 13:
									rd.accountNo=cell.value;
									break;
							}
						}
						if (!isNaN(parseInt(rd.start)) && !isNaN(parseInt(rd.end)))
						{
							recordArr.push(rd);
							tableModel.addRow(new Array(rd.nick, rd.prj, rd.num, rd.amt, rd.amt1, rd.unFee, rd.unAmt, rd.merFee, rd.merAmt, rd.bank, rd.account, rd.accountNo));
							total+=parseFloat(rd.merAmt);
						}
					}
					this.lbMerAmtTotal.setText("累计金额【   " + total + "   】");
				}

				*/

		private function onExport(e:AWEvent):void
		{
			if (this.recordArr != null && this.recordArr.length > 0)
			{
				var txt:File=File.documentsDirectory;
				txt.browseForDirectory("请指定要保存文件的位置");
				txt.addEventListener(Event.SELECT, onSelected);
			}
			else
			{
				JOptionPane.showMessageDialog("提示：", "请先导入清算汇总文件   ", null, (this.parent.parent as JNativeFrame), true);
			}
		}

		private function onSelected(e:Event):void
		{
			var content:String="";
			for each (var r:com.lakala.bank.Record in this.recordArr)
			{
				content+=StringUtils.trim(r.accountNo) + "|" + StringUtils.trim(r.merAmt) + "|" + StringUtils.trim(r.account) + "|" + "\r\n";
			}

			var file:File=new File((e.target as File).nativePath).resolvePath("温州银行网银批量文件.txt");
			var fileStream:FileStream=new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeUTFBytes(content);
			fileStream.close();
			JOptionPane.showMessageDialog("提示：", "文件导出成功!   ", null, (this.parent.parent as JNativeFrame), true);
			this.tableModel.setRowCount(0);
			this.recordArr=null;
			this.lbMerAmtTotal.setText("");
		}

	}
}
